package org.maxkey.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.maxkey.portal.core.cam.PageResultData;
import org.maxkey.portal.core.cam.ResultData;
import org.maxkey.portal.core.search.ParamModel;
import org.maxkey.portal.core.search.model.DocParam;
import org.maxkey.portal.model.Doc;
import org.maxkey.portal.model.UserInfo;
import org.maxkey.portal.repository.DocRepository;
import org.maxkey.portal.repository.UserInfoRepository;
import org.maxkey.portal.service.DocService;
import org.maxkey.portal.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DocServiceImpl implements DocService {

    @Autowired
    DocRepository docfoRepository;

    @Override
    public ResultData findById(Integer id) {
        try {
            if(id==null){
                return ResultData.error("参数错误");
            }
            Optional<Doc> u= docfoRepository.findById(id.intValue());
            if(!u.isEmpty()){
                return ResultData.ok(u);
            }
            return ResultData.error("查询不存在");
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }
    @Override
    public ResultData findAll() {
        try {
            return ResultData.ok(docfoRepository.findAll());
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public PageResultData findList(ParamModel<DocParam> searData) {
        try {
            PageRequest pageRequest = PageRequest.of(searData.getPageNo(), searData.getPageSize());
            Specification<Doc> specification = (Specification<Doc>) (root, query, criteriaBuilder) -> {
                List<Predicate> list = new ArrayList<>();
                if(!StringUtils.isEmpty(searData.getCondition().getTitle())){
                    Predicate predicate1 = criteriaBuilder.like(root.get("title").as(String.class), "%" + searData.getCondition().getTitle() + "%");
                    list.add(predicate1);
                }
                if(searData.getCondition().getType()!=null){
                    Predicate predicate1 = criteriaBuilder.equal(root.get("type").as(Integer.class), searData.getCondition().getType());
                    list.add(predicate1);
                }
                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            };
            Page<Doc> p=docfoRepository.findAll(specification,pageRequest);
            return PageResultData.ok(Integer.valueOf(p.getTotalElements()+"") ,p.getContent());
        }catch (Exception e){
            e.printStackTrace();
            return PageResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData deleteById(Long id) {
        try {
            if(id==null){
                return ResultData.error("参数错误");
            }
            docfoRepository.deleteById(id.intValue());
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData save(Doc model) {
        try {
            if(model.getCreateTime()==null){
                model.setCreateTime(new Date());
            }
            if(model.getUpdateTime()==null){
                model.setUpdateTime(new Date());
            }
            if(model.getPublicTime()==null){
                model.setPublicTime(new Date());
            }
            if(StringUtils.isEmpty(model.getVersion())){
                model.setVersion("1.0");
            }
            if(model.getOnclickNum()==null){
                model.setOnclickNum(0);
            }
            docfoRepository.save(model);
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

}
